<template>
	<view class="">
		<!-- <view class="bg">
			<image class="typebg" :src="bg" mode=""></image>
			<view class="icon-box">
				<image :src="icon" mode=""></image>
				<view class="hint">
					{{hint}}
				</view>
			</view>
		</view> -->
		<view class="list">
			<radio-group @change="radioChange">
				<view @tap="tapChange(item,index)" class="item" :class="list.length-1 == index"
					v-for="(item,index) in list" :key="index">
					<view class="">
						<view class="title">
							{{item.title}}
						</view>
						<view class="msg">
							{{item.msg}}
						</view>
					</view>
					<view class="">
						<radio :value="item.type" color="#FF0025" :checked="item.checked" />
					</view>
				</view>
			</radio-group>
		</view>
	</view>
</template>

<script>
	import {
		RetCodeMsg,
		CodeMsg
	} from '@/utils/errorCode.js'
	import deviceLogin from '@/utils/deviceLogin.js';
	export default {
		data() {
			return {
				// bg: "https://statics.xmcsrv.net/weixin/device/notOpened_bg.png",
				// icon: "https://statics.xmcsrv.net/weixin/device/notOpenedIcon.png",
				bg: "https://statics.xmcsrv.net/weixin/device/open_bg.png",
				icon: "https://statics.xmcsrv.net/weixin/device/openIcon_bg.png",
				hint: '提醒已开启',
				list: [{
					title: '全部消息联动',
					msg: "触发任意事件时，图片都会上传云端",
					checked: false,
					Picture: {
						Mode: 0
					},
					Video: {
						Mode: 0
					},
					type: 'importance'
				}, {
					title: '重要消息联动',
					msg: "触发重要事件时，图片才会上传云端",
					checked: false,
					Picture: {
						Mode: 1
					},
					Video: {
						Mode: 1
					},
					type: 'all'
				}],
				device: '',
				tryTimeOutCount: 0
			}
		},
		onLoad(opt) {
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			// this.getUserAlarm()
			this.getLogin()
			if (uni.getStorageSync(this.device.deviceNo)['NetWork.PMS']) {
				let info = uni.getStorageSync(this.device.deviceNo)['NetWork.PMS'].CloudCfg;
				this.list.forEach((item, index) => {
					// 重要消息
					if (info.Picture.Mode == 1 && info.Video.Mode == 1) {
						if (item.type == 'all') {
							item.checked = true
						}
					}
					// 全部消息
					if (info.Picture.Mode == 0 && info.Video.Mode == 0) {
						if (item.type == 'importance') {
							item.checked = true
						}
					}
				})
			}
		},
		methods: {
			getUserAlarm() {
				let data = {
					deviceSn: this.device.deviceNo,
				};
				getApp().globalData.api.getUserAlarm(data, (result) => {
					console.log(result, 'getUserAlarm')
					if (result.lvl == 'full' || result.lvl == 'important') {
						this.bg = "https://statics.xmcsrv.net/weixin/device/open_bg.png"
						this.icon = "https://statics.xmcsrv.net/weixin/device/openIcon_bg.png"
						this.hint = '提醒已开启'
						return
					}
					if (result.lvl == 'no') {
						this.bg = "https://statics.xmcsrv.net/weixin/device/notOpened_bg.png"
						this.icon = "https://statics.xmcsrv.net/weixin/device/notOpenedIcon.png"
						this.hint = '提醒已关闭'
						return
					}
				});
			},
			radioChange(e) {
				console.log(e)
				this.setconfig(e.detail.value)
			},
			tapChange(e, index) {
				this.setconfig(e.type)
				this.list.forEach((item, indes) => {
					if (item.type == e.type) {
						item.checked = true
					} else {
						item.checked = false
					}
				})
			},
			getLogin() {
				deviceLogin(this.device, (result) => {
					if (result.code == 4120 || result.code == 4101) {
						return uni.showModal({
							content: '设备可能处于离线或深度休眠中，请手动唤醒设备后重试',
							showCancel: false,
							confirmColor: '#FF0025',
							confirmText: '确定',
							success: (res) => {
								uni.navigateBack()
							}
						});
					}
					this.getconfig()
				})
			},
			// 获取消息
			getconfig() {
				uni.showLoading({
					mask: true
				});
				let token = this.device.token;
				let data = {
					Name: 'NetWork.PMS',
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					// console.log(result)
					let results = result.data.data
					console.log(result.data, 'getconfig')
					uni.hideLoading()
					if (results.Ret == 100) {
						let info = results['NetWork.PMS'].CloudCfg;
						this.list.forEach((item, index) => {
							// 重要消息
							if (info.Picture.Mode == 1 && info.Video.Mode == 1) {
								if (item.type == 'all') {
									item.checked = true
								}
							}
							// 全部消息
							if (info.Picture.Mode == 0 && info.Video.Mode == 0) {
								if (item.type == 'importance') {
									item.checked = true
								}
							}
						})
					} else if (results.Ret == 102) {
						uni.showModal({
							content: '当前设备不支持消息联动设置！',
							showCancel: false,
							confirmColor: '#FF0025',
							confirmText: '确定',
							success: (res) => {
								uni.navigateBack()
							}
						})
					} else {
						uni.showToast({
							title: RetCodeMsg[results.Ret] || results.RetMsg,
							icon: 'none',
							duration: 2000
						});
					}
				}, false, false);
			},
			// 设置消息
			setconfig(type) {
				uni.showLoading({
					mask: true
				});
				let token = this.device.token;
				let data = {
					Name: 'NetWork.PMS',
					"NetWork.PMS": {
						"CloudCfg": {
							"Picture": {
								"Mode": type == 'importance' ? 0 : 1 // 推图模式  0：全部消息推图 1：重要消息推图 2:全部消息不推图       
							},
							"Video": {
								"Mode": type == 'importance' ? 0 : 1 // 推视频模式 0：全部消息视频  1：重要消息推视频 2:全部消息不推视频
							},
						}
					}
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					console.log(result)
					uni.getStorageSync(this.device.deviceNo);
					console.log(uni.getStorageSync(this.device.deviceNo), '获取设备信息')
					let device = uni.getStorageSync(this.device.deviceNo)
					device['NetWork.PMS'] = data['NetWork.PMS'];
					console.log(data, device)
					uni.setStorageSync(this.device.deviceNo, data)
					uni.hideLoading()
					uni.showToast({
						title: '保存成功',
						icon: 'none',
						duration: 2000
					});
				});
			},
		}
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		height: 627rpx;
		position: relative;
	}

	.typebg {
		width: 100%;
		height: 100%;
	}

	.icon-box {
		position: absolute;
		top: 40%;
		margin-top: -120rpx;
		left: 50%;
		margin-left: -120rpx;

		>image {
			width: 242rpx;
			height: 242rpx;
		}

		.hint {
			font-size: 40rpx;
			color: #fff;
			text-align: center;
			margin-top: 20px;
		}
	}

	.list {
		border-radius: 15rpx;
		overflow: hidden;
		width: 94%;
		margin: auto;
		margin-top: 20rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		// position: fixed;
		// top: 560rpx;
		// left: 3%;

		.item {
			margin: auto;
			height: 160rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #eee;

			.title {
				font-size: 30rpx;
				color: #000;
			}

			.msg {
				margin-top: 22rpx;
				font-size: 24rpx;
				color: #898989;
			}
		}

		.item:last-child {
			border: 0;
		}
	}
</style>
